from django.contrib import admin
from .models import (
    GitLabServer, Project, Environment, UserProjectPermission,
    GitLabProject, GitLabUser, GitLabAudit, DevOpsTimeline
)


@admin.register(GitLabServer)
class GitLabServerAdmin(admin.ModelAdmin):
    list_display = ['name', 'url', 'is_active', 'status', 'project_count', 'user_count', 'last_check']
    list_filter = ['is_active', 'status']
    search_fields = ['name', 'url']
    readonly_fields = ['status', 'project_count', 'user_count', 'last_sync', 'last_check', 'created_at', 'updated_at']


@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'is_active', 'created_at']
    list_filter = ['is_active']
    search_fields = ['name', 'code']


@admin.register(Environment)
class EnvironmentAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'is_active', 'created_at']
    list_filter = ['is_active']
    search_fields = ['name', 'code']


@admin.register(UserProjectPermission)
class UserProjectPermissionAdmin(admin.ModelAdmin):
    list_display = ['user', 'project', 'environment', 'can_view', 'can_edit', 'can_delete', 'can_deploy']
    list_filter = ['project', 'environment', 'can_view', 'can_edit', 'can_delete', 'can_deploy']
    search_fields = ['user__username', 'project__name', 'environment__name']


@admin.register(GitLabProject)
class GitLabProjectAdmin(admin.ModelAdmin):
    list_display = ['name', 'gitlab_server', 'project', 'environment', 'visibility', 'star_count', 'last_activity_at']
    list_filter = ['gitlab_server', 'project', 'environment', 'visibility']
    search_fields = ['name', 'path', 'path_with_namespace']
    readonly_fields = ['gitlab_id', 'created_at', 'updated_at']


@admin.register(GitLabUser)
class GitLabUserAdmin(admin.ModelAdmin):
    list_display = ['username', 'name', 'email', 'gitlab_server', 'local_user', 'state', 'is_admin']
    list_filter = ['gitlab_server', 'state', 'is_admin']
    search_fields = ['username', 'name', 'email']
    readonly_fields = ['gitlab_id', 'created_at', 'updated_at']


@admin.register(GitLabAudit)
class GitLabAuditAdmin(admin.ModelAdmin):
    list_display = ['operation_type', 'target_name', 'user', 'gitlab_server', 'result', 'created_at']
    list_filter = ['operation_type', 'result', 'gitlab_server']
    search_fields = ['target_name', 'description', 'user__username']
    readonly_fields = ['created_at']


@admin.register(DevOpsTimeline)
class DevOpsTimelineAdmin(admin.ModelAdmin):
    list_display = ['event_type', 'title', 'project', 'environment', 'user', 'status', 'created_at']
    list_filter = ['event_type', 'project', 'environment', 'status']
    search_fields = ['title', 'description', 'user__username']
    readonly_fields = ['created_at']
